package com.hyg.lib_common.loginpart;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.EventBusMessage;
import com.hyg.lib_common.DataModel.ReceiveData;
import com.hyg.lib_common.DataModel.YangShengInfo.ReceiveDataInt;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.constant.UrlUtils;
import com.hyg.lib_common.R;
import com.hyg.lib_common.WebView.WebViewActivity;
import com.hyg.lib_common.databinding.ActivityLoginByPasswordBinding;
import com.hyg.lib_common.loginpart.modle.LoginByPasswordData;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;

public class LoginByPasswordActivity extends BaseActivity {

    ActivityLoginByPasswordBinding binding;
    boolean isSee = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginByPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    public void init() {
        initView();
    }


    @SuppressLint("ClickableViewAccessibility")
    public void initView() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(LoginByPasswordActivity.this, WebViewActivity.class);
                it.putExtra("url", UrlUtils.PrivacyUrl);
                it.putExtra("title", "隐私政策与免责声明");
                it.putExtra("type", "Privacy");
                startActivity(it);
            }
        });

        binding.ivSeePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSee) {
                    Log.i("lgb", "onClick: ==");
                    isSee = !isSee;
                    binding.ivSeePassword.setImageResource(R.mipmap.hide_password_icon);
                    binding.etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    Log.i("lgb", "onClick: !=");
                    isSee = !isSee;
                    binding.ivSeePassword.setImageResource(R.mipmap.see_password_icon);
                    binding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });

        binding.btnLoginPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = binding.etPhone.getText().toString();
                String password = binding.etPassword.getText().toString();
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginByPasswordActivity.this, "请正确填写用户名和密码", Toast.LENGTH_SHORT).show();
                    return;
                }
//                loginLoading(0);


                
                CommonHttpUtil.loginByPassword(phone, password, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ErrorDialog("连接服务器失败！");
                            }
                        });
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result=response.body().string();
                        ReceiveDataInt receiveData = JSONObject.parseObject(result, ReceiveDataInt.class);
                        if(receiveData.getCode()==200){
                            LoginByPasswordData loginByPasswordData=JSONObject.parseObject(receiveData.getData(),LoginByPasswordData.class);
                            if(loginByPasswordData.getCode()==200){
                                editorn = sn.edit();
                                editorn.putString("phone", loginByPasswordData.getData().getUserInfo().getPhone());
                                editorn.putString("token", loginByPasswordData.getData().getToken());
                                editorn.apply();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ToIntent(Constants.PATH_ACTIVITY_MAIN, true);
                                        EventBus.getDefault().post(new EventBusMessage("startMainActivity"));
                                    }
                                });
                            }else{
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ErrorDialog(loginByPasswordData.getData().getMessage());
                                    }
                                });
                            }
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ErrorDialog(receiveData.getMessage());
                                }
                            });
                        }
                    }
                });
            }
        });
    }


    /* 登录提示对话框 */
    public void loginLoading(int numType) {
        if (pDialog.isShowing())
            pDialog.dismiss();
        if (numType == 0) {
            View view = View.inflate(this, R.layout.dialog_loading, null);
            TextView loadingText = (TextView) view.findViewById(R.id.loadingText);
            //            SpinKitView spin_kit = (SpinKitView)view.findViewById(R.id.spin_kit);
            loadingText.setText("正在登录中...");
            pDialog = new SweetAlertDialog(LoginByPasswordActivity.this, SweetAlertDialog.NORMAL_TYPE);
            pDialog.setCustomView(view);
            pDialog.setConfirmButton("好的", new SweetAlertDialog.OnSweetClickListener() {
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismiss();
                }
            });
        } else if (numType == 1) {
            pDialog = new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE);
            pDialog.setTitleText("登录成功");
        } else {
            pDialog = new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE);
            pDialog.setTitleText("登录失败");
        }
        pDialog.show();
    }

}